package com.xth8013.as.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xth8013.as.domain.Role;
import com.xth8013.as.service.RoleService;
import com.xth8013.as.util.Message;
import com.xth8013.as.util.ObjectUtils;
import com.xth8013.as.util.RoleQueryHelper;

/**
 * 
 * @Description:
 * @author: tianhua.xie
 * @Date: 2017年4月3日 下午9:39:15
 *
 */
@Controller
@RequestMapping("/roleMgrController")
public class RoleMgrController extends BaseController {

	@Autowired
	private RoleService roleService;

	@RequestMapping("/roleMgr")
	public String loadRoleList() {

		return "role/role_list";
	}

	@RequestMapping(value = "/listJson")
	@ResponseBody
	public Map<String, Object> listJson(RoleQueryHelper helper, HttpServletRequest request) {

		if (ObjectUtils.isBlank(helper)) {
			helper = new RoleQueryHelper();
			helper.setQryRoleStatus("1");
		}
		System.out.println(helper);
		
		// String pageNo = request.getParameter("page");
		// String pageSize = request.getParameter("rows");
		// String qryRoleName = request.getParameter("qryRoleName");
		// String qryRoleRemark = request.getParameter("qryRoleRemark");
		// String qryRoleStatus = request.getParameter("qryRoleStatus");
		// String orderField = request.getParameter("sort");
		// String orderType = request.getParameter("order");
		//
		// if (ObjectUtils.isNotBlank(pageNo)) {
		// helper.setPageNo(Integer.parseInt(pageNo));
		// }
		// if (ObjectUtils.isNotBlank(pageSize)) {
		// helper.setPageSize(Integer.parseInt(pageSize));
		// }
		// if (ObjectUtils.isNotBlank(qryRoleName)) {
		// helper.setQryRoleName(qryRoleName);
		// }
		// if (ObjectUtils.isNotBlank(qryRoleRemark)) {
		// helper.setQryRoleRemark(qryRoleRemark);
		// }
		// if (ObjectUtils.isNotBlank(qryRoleStatus)) {
		// helper.setQryRoleStatus(qryRoleStatus);
		// }
		// if (ObjectUtils.isNotBlank(orderField)) {
		// helper.setOrderField(orderField);
		// }
		// if (ObjectUtils.isNotBlank(orderType)) {
		// helper.setOrderType(orderType);
		// }

		Map<String, Object> dataMap = null;
		try {
			dataMap = this.roleService.listRole(helper);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return dataMap;
	}

	@RequestMapping(value = "/preAdd")
	public ModelAndView preAdd() {
		// System.out.println("aa");
		ModelAndView mv = new ModelAndView();
		mv.setViewName("role/role_add");
		return mv;
	}

	@RequestMapping(value = "/loadMenuTree")
	@ResponseBody
	public List<Map<String, Object>> loadMenuTree(HttpServletRequest request) {
		List<Map<String, Object>> treeData = this.roleService.loadMenuTree();
		return treeData;
	}

	@RequestMapping(value = "/loadRoleMenuTree/{roleId}")
	@ResponseBody
	public List<Map<String, Object>> loadRoleMenuTree(@PathVariable(value = "roleId") Integer roleId) {
		List<Map<String, Object>> treeData = null;
		try {

			treeData = this.roleService.loadRoleMenuTree(roleId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return treeData;
	}

	@RequestMapping(value = "/addRole")
	@ResponseBody
	public Message save(Role role, String roleMenuData) throws Exception {

		Message message = null;

		// System.out.println(role);
		// System.out.println(roleMenuData);

		try {
			this.roleService.save(role, roleMenuData);
			message = coverMessage("200", "保存成功");
		} catch (Exception e) {
			message = coverMessage("500", e.getMessage());
		}

		return message;
	}

	@RequestMapping(value = "/preModify/{roleId}")
	public String preModify(@PathVariable(value = "roleId") Integer roleId, ModelMap modelMap) {
		Role role = this.roleService.getRoleById(roleId);
		modelMap.put("role", role);
		return "role/role_edit";
	}

	@RequestMapping(value = "/modifyRole")
	@ResponseBody
	public Message modifyRole(Role role, String roleMenuData) throws Exception {
		System.out.println(roleMenuData);
		Message message = null;

		try {
			this.roleService.modifyRole(role, roleMenuData);
			message = coverMessage("200", "修改成功");
		} catch (Exception e) {
			message = coverMessage("500", e.getMessage());
			e.printStackTrace();
		}

		return message;
	}

	@RequestMapping(value = "/deleteRole")
	@ResponseBody
	public Message deleteRole(String decRoleIds) throws Exception {
		System.out.println(decRoleIds);
		Message message = null;
		
		try {
			this.roleService.delectRoles(decRoleIds);
			message = coverMessage("200", "删除成功");
		} catch (Exception e) {
			message = coverMessage("500", e.getMessage());
			e.printStackTrace();
		}
		
		return message;
	}
}
